<template>

  <el-dialog :title="id ? '集合修改' : '集合添加'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="30%">

    <el-form ref="main-form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="集合名称" size="mini" prop="collectionName">
        <el-input placeholder="" v-model="form.collectionName" @keyup.enter.native="submit" clearable style="width: 250px;"></el-input>
      </el-form-item>

      <el-form-item label="类型" size="mini" prop="type">
        <el-select v-model="form.type" placeholder="未选择" :disabled="id != null" style="width: 250px;">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="关联规则" size="mini" prop="blackType">
        <el-select v-model="form.conn" placeholder="未选择" style="width: 250px;" clearable multiple filterable>
          <el-option
            v-for="item in ruleIdOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-popover
        v-if="id != null"
        placement="top"
        width="160"
        v-model="popDeleteVisible">
        <p v-if="form.status == 1">确定禁用吗？</p>
        <p v-if="form.status == 0">确定启用用吗？</p>
        <div style="text-align: right; margin: 0">
          <el-button size="mini" type="default" @click="popDeleteVisible = false">取消</el-button>
          <el-button :type="form.status == 1 ? 'danger' : 'primary'" size="mini" @click="submitDel">确定</el-button>
        </div>
        <el-button v-if="form.status == 1" type="danger" slot="reference"  size="small" style="float: left;" :loading="loading">禁 用</el-button>
        <el-button v-if="form.status == 0" type="success" slot="reference"  size="small" style="float: left;" :loading="loading">启 用</el-button>
      </el-popover>

      <el-button @click="close" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small" :loading="loading">提 交</el-button>
    </div>

  </el-dialog>
</template>

<script>

import ApiMain from '@/api/risk.collection';

export default {
  mixins: [
  ],
  props: [
    'ruleIdOptions',
  ],
  data () {
    return {

      dialogVisible: false,
      popDeleteVisible: false,
      loading: false,

      form: {
        collectionName: '',
        type: 2,
        status: 1,
        conn: [],
      },

      rules: {
        collectionName: [
          { required: true, message: '请输入集合名称', trigger: 'blur' },
        ],
        // conn: [
        //   { required: true, message: '请选择规则', trigger: 'blur' },
        // ],
      },

      id  : null,
      data: null,

      typeOptions: [
        {label: '系统风控', value: 1},
        {label: '商户风控', value: 2},
        {label: '支付通道风控', value: 3},
        // {label: '可疑风控', value: 4}
      ]
    }
  },
  mounted () {
  },

  methods: {

    show (id = null) {

      this.id = id;

      if (this.id) {
        this.fetchData();
      }
      else {
        this.reset();
      }

      this.dialogVisible = true;
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.form = {
        collectionName: '',
        type: 2,
        status: 1,
        conn: [],
      };
    },

    fetchData () {

      ApiMain.info({id: this.id})
      .then(re => {
        this.data = re; 
      })
      .catch(() => {
        this.data = null;
      })
      .finally(() => {
        this.renderData();
      });
    },

    renderData () {
      this.form.collectionName = this.data.collectionName;
      this.form.type = this.data.type;
      this.form.status = this.data.status;
      this.form.conn = this.data.ruleList.map(item => item.id);
      console.log(this.form)
    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      try {
        if (this.id) {
          await this.submitSet();
        }
        else {
          await this.submitAdd();
        }
      } finally {
        this.loading = false;
      }
      
    },

    async submitDel() {

      this.popDeleteVisible = false;
      this.loading = true;

      try {
        await ApiMain.updateStatus({id: this.id, status: this.form.status == 1 ? 0 : 1});

        this.loading = false;
        this.close();
        this.$message({message: '删除成功', type: 'success'});
      } catch (e) {
        this.loading = false;
      }
    },

    async submitSet () {

      this.form.id = this.id;
      let ret = await ApiMain.set(this.form);
      console.log(ret)
      this.$message({message: '修改成功', type: 'success'});
      this.close();

    },

    async submitAdd () {

      await ApiMain.add(this.form);
      this.$message({message: '添加成功', type: 'success'});
      this.close();
    },
  }
}
</script>

<style>

</style>
